#pragma once
#include <iostream>
#include <functional>
#include "log.hpp"
using namespace std;
void serviceIO(const int &fd)
{
    while (1)
    {
        char buffer[1024];
        ssize_t size = read(fd, buffer, sizeof(buffer) - 1);
        if (size > 0)
        {
            buffer[size] = 0;
            cout << "[Client] : " << buffer << endl;
            string messages = buffer;
            ssize_t ret = write(fd, messages.c_str(), messages.size());
            if (ret < 0)
            {
                cerr << strerror(errno) << endl;
            }
        }
        else if (size == 0)
        {
            logMessage(NORMAL, "Client quit");
            break;
        }
        else
        {
            cerr << strerror(errno) << endl;
            exit(3);
        }
    }
    close(fd);
}
class Task
{
    typedef function<void(int)> func_t;

public:
    Task() {}
    Task(const int &socket, func_t func) : _fd(socket), _func(func)
    {
    }
    void operator()()
    {
        _func(_fd);
    }

private:
    int _fd;
    func_t _func;
};
